<template>
    <div>
        <el-table :data="store.getters['menu/getMenuList']" border style="width: 100%" ref="menuRef"
            :tree-props="{ children: 'children' }" row-key="id">
            <el-table-column prop="id" label="菜单编号" width="180" />
            <el-table-column prop="title" label="菜单名称" width="180" />
            <el-table-column prop="pid" label="上级菜单" />
            <el-table-column label="菜单图标">
                <template   v-slot="item">
                    <div>
                        <el-icon>
                            <!-- 动态渲染组件 -->
                            <component v-if="item.row.icon" :is="item.row.icon"></component>
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="url" label="菜单地址" />
            <el-table-column prop="status" label="状态">
                <template v-slot="item">
                    <div>
                        {{ item.row.status == 1 ? '启用' : '禁用' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template v-slot="item">
                    <div>
                        <el-button type="primary" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script lang='ts' setup>
// 引入内置方法
import { ref, defineEmits } from 'vue';
import type { FormInstance, FormRules } from 'element-plus'
import { useStore } from 'vuex';

// 引入消息提示方法
import { ElMessage, ElMessageBox } from 'element-plus';
// 引入接口
import { menudelete } from '../../request/api';

// 通过ref获取dom节点
let menuRef = ref<FormInstance>();
// 调用子传父方法 
let emit = defineEmits(['edit', 'del']);
// 编辑
const edit = (id: number) => {
    console.log('编辑');
    // 通知父元素弹出弹窗
    emit('edit', id);
}

// 调用仓库方法
let store = useStore();
//调用列表
store.dispatch('menu/getMenulistActions');

// 删除
const del = (id: number) => {
    ElMessageBox.confirm(
        '确定要删除此数据吗?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            menudelete({ id }).then((res: any) => {
                if (res.code == 200) {
                    // 删除成功
                    ElMessage.success(res.msg)
                    // 重新渲染视图
                    store.dispatch('menu/getMenulistActions');
                } else {
                    // 删除失败
                    ElMessage.error(res.msg)

                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消删除',
            })
        })

}


</script>
<style scoped lang='less'></style>